package com.fengjing.launcher.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Outline;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Toast;
import android.widget.VideoView;

import com.fengjing.launcher.R;
import com.fengjing.launcher.parse.AdParser;
import com.fengjing.launcher.service.CheckService;
import com.haijing.highglass.HmdContrast;
import com.haijing.highglass.HmdCtrl;
import com.haijing.highglass.HmdSaturation;

/**
 * Created by admin on 2017/9/22.
 */

public class EyeActivity extends BaseActivity implements View.OnClickListener {
    private long time;
    int count = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eye);
        HmdCtrl.setContrast(new HmdContrast(50, 50, 50, 50));
        HmdCtrl.setSaturation(new HmdSaturation(42, 42, 42, 42));

        ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),15);
            }
        };
        findViewById(R.id.btn_01).setClipToOutline(true);
        findViewById(R.id.btn_02).setClipToOutline(true);
        findViewById(R.id.btn_01).setOnClickListener(this);
        findViewById(R.id.btn_02).setOnClickListener(this);
        findViewById(R.id.btn_01).setOutlineProvider(viewOutlineProvider);
        findViewById(R.id.btn_02).setOutlineProvider(viewOutlineProvider);
        startService(new Intent(EyeActivity.this, CheckService.class));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_01:
                findViewById(R.id.foc_layout).setVisibility(View.GONE);
                findViewById(R.id.pup_layout).setVisibility(View.VISIBLE);
                findViewById(R.id.btn_02).requestFocus();
                break;
            case R.id.btn_02:

                findViewById(R.id.pup_layout).setVisibility(View.GONE);
                VideoView videoView = (VideoView) findViewById(R.id.video_view);
                videoView.setVisibility(View.VISIBLE);
                try {
                    videoView.setVideoPath(AdParser.getBannerVideoPath());
                }catch (Exception e){
                    Toast.makeText(this, "广告文件路径不存在！！", Toast.LENGTH_SHORT).show();
                    finish();
                }
                videoView.start();
                videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        finish();
                    }
                });
                videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        finish();
                        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
                        Toast.makeText(EyeActivity.this, "广告视频播放错误！！", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                SharedPreferences sharedPreferences = getSharedPreferences("record", MODE_PRIVATE);
                boolean hasInit = sharedPreferences.getBoolean("hasInit", false);
                if (!hasInit) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("hasInit", true);
                    editor.putLong("initTime", System.currentTimeMillis());
                    editor.commit();
                }

                break;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN&&findViewById(R.id.btn_01).hasFocus()) {
            if (System.currentTimeMillis() - time < 1000) {
                ++count;

            } else {
                count = 0;
            }
            time = System.currentTimeMillis();
            if (count > 5) {
                count = 0;
                Intent intent = new Intent(EyeActivity.this, CITActivity.class);
                startActivity(intent);
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (findViewById(R.id.pup_layout).getVisibility() == View.VISIBLE){
                findViewById(R.id.foc_layout).setVisibility(View.VISIBLE);
                findViewById(R.id.btn_01).requestFocus();
                findViewById(R.id.pup_layout).setVisibility(View.GONE);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
